<?php

namespace app\common\model;

use think\Db;
use think\Exception;
use think\Model;

class OrderPdd extends Model
{
    // 表名
    protected $name = 'order_pdd';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'order_create_time_text',
        'order_pay_time_text',
        'order_group_success_time_text',
        'order_receive_time_text',
        'order_verify_time_text',
        'order_settle_time_text',
        'verify_time_text',
	    'order_status_text',
	    'type_text',
	    'status_text'
    ];
    

    const STATUS_N = 0;
    const STATUS_W = 1;
    const STATUS_Y = 2;
    
    public static $statusList = [
        self::STATUS_Y => '已返利',
	    self::STATUS_W => '待返利',
	    self::STATUS_N => '待拼多多结算'
    ];
	
	public function getStatusTextAttr($value, $data) {
		$value = $value ? $value : $data['status'];
		$list = self::$statusList;
		return isset($list[$value]) ? $list[$value] : '';
	}
    
    CONST ORDER_STATUS = -1;
	CONST ORDER_STATUS_0 = 0;
	CONST ORDER_STATUS_1 = 1;
	CONST ORDER_STATUS_2 = 2;
	CONST ORDER_STATUS_3 = 3;
	CONST ORDER_STATUS_4 = 4;
	CONST ORDER_STATUS_5 = 5;
	CONST ORDER_STATUS_8 = 8;
	CONST ORDER_STATUS_10 = 10;
    
    public static $orderStatusList = [
        self::ORDER_STATUS => '未支付',
	    self::ORDER_STATUS_0 => '已支付',
	    self::ORDER_STATUS_1 => '已成团',
	    self::ORDER_STATUS_2 => '确认收货',
	    self::ORDER_STATUS_3 => '审核成功',
	    self::ORDER_STATUS_4 => '审核失败',
	    self::ORDER_STATUS_5 => '已经结算',
	    self::ORDER_STATUS_8 => '非多多进宝商品',
	    self::ORDER_STATUS_10 => '已处罚',
    ];

    public function getOrderStatusTextAttr($value, $data) {
        $value = $value ? $value : $data['order_status'];
        $list = self::$orderStatusList;
        return isset($list[$value]) ? $list[$value] : '';
    }
    
    CONST TYPE_0 = 0;
	CONST TYPE_1 = 1;
	CONST TYPE_2 = 2;
	CONST TYPE_54 = 54;
	CONST TYPE_55 = 55;
	CONST TYPE_56 = 56;
	CONST TYPE_13 = 13;
	
	public static $typeList = [
		self::TYPE_1 => '红包活动推广',
		self::TYPE_0 => '单品（领券页）推广',
		self::TYPE_2 => '领券页底部推荐',
		self::TYPE_54 => '大转盘主页的商品',
		self::TYPE_55 => '抽中免单的商品',
		self::TYPE_56 => '大转盘抽中红包的商品',
		self::TYPE_13 => '大转盘拉新锁佣的',
	];

	
	public function getTypeTextAttr($value, $data) {
		$value = $value ? $value : $data['type'];
		$list = self::$typeList;
		return isset($list[$value]) ? $list[$value] : '';
	}
    
    public function getOrderCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['order_create_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getOrderPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['order_pay_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getOrderGroupSuccessTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['order_group_success_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getOrderReceiveTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['order_receive_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getOrderVerifyTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['order_verify_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getOrderSettleTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['order_settle_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getVerifyTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['verify_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setOrderCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setOrderPayTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setOrderGroupSuccessTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setOrderReceiveTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setOrderVerifyTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setOrderSettleTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setVerifyTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }
	
	
	/**
	 * 拼多多佣金返利
	 * @param $order 订单信息
	 * @param $config 返利配置
	 * @return bool
	 * @throws \think\exception\PDOException
	 */
    public function rebate($order){
        $this->startTrans();
        try {
            //更改状态
	        $orderRes = $this->save([
	            'status' => self::STATUS_Y,
	        ], ['id' => $order['id']]);
	        
	        if(!$orderRes) {
	        	throw new Exception('更改订单状态出错');
	        }
	        
	
	        //查找已经生成的待返利记录
	        $rebateModel = new RebateRecord();
	        $rebateRes = $rebateModel->rebate($order['order_sn'], $rebateModel::TYPE_PDD);
	        if(!$rebateRes) {
		        throw new Exception($rebateModel->getError());
	        }
	
	        $this->commit();
	        return true;
	        
        } catch (Exception $e) {
        	$this->error = $e->getMessage();
        	$this->rollback();
        	return false;
        }
    }
    
    
    public function addRow($order, $rule){
        $this->startTrans();
        try {
        	//添加记录
	        $res = Db::name('orderPdd')->insert($order);
			if(!$res) {
				throw new Exception('添加记录出错');
			}
	
	        //如果有设置返利规则，且返利金额大于0，进行返利操作
	        if($rule && $order['promotion_amount']/100 > 0) {
		        $model = new CommissionRebate();
		        $recordRes = $model->addRebateRocord($order['merid'], $order['goods_id'], $order['promotion_amount']/100, $order['user_id'],$rule, $order['order_sn'], RebateRecord::TYPE_PDD, '拼多多');
		
		        if(!$recordRes) {
			        throw new Exception('发送奖励出错：'.$model->getError());
		        }
	        }
        	
        	$this->commit();
        	return true;
        } catch (Exception $e) {
        	$this->error = $e->getMessage();
        	$this->rollback();
        	return false;
        }
    }
}
